<template>
	<!-- 为你推荐 -->
	<view class="hot-goods u-m-b-10 u-p-x-16">
		<view class="u-waterfall">
			<view id="u-left-column" class="u-column">
				<view class="goods-item u-m-b-16 u-flex u-row-center u-col-center" v-for="leftGoods in leftList"
					:key="leftGoods.id">
					<ljshop-goods-card :detail="leftGoods" :type="leftGoods.activity_type" :image="leftGoods.large"
						:title="leftGoods.name" :subtitle="leftGoods.caption" :price="leftGoods.price.toFixed(2)"
						:originPrice="((leftGoods.price-leftGoods.cost)*rebate/100).toFixed(2)" :sales="leftGoods.sales"
						:tagTextList="leftGoods.activity_discounts_tags"
						@click="$Router.push({ path: '/pages/goods/detail', query: { id: leftGoods.id } })">
					</ljshop-goods-card>
				</view>
			</view>
			<view id="u-right-column" class="u-column">
				<view class="goods-item  u-m-b-16 u-flex u-row-center u-col-center" v-for="rightGoods in rightList"
					:key="rightGoods.id">
					<ljshop-goods-card :detail="rightGoods" :type="rightGoods.activity_type" :image="rightGoods.large"
						:title="rightGoods.name" :subtitle="rightGoods.caption" :price="rightGoods.price.toFixed(2)"
						:originPrice="((rightGoods.price-rightGoods.cost)*rebate/100).toFixed(2)" :sales="rightGoods.sales"
						:tagTextList="rightGoods.activity_discounts_tags"
						@click="$Router.push({ path: '/pages/goods/detail', query: { id: rightGoods.id } })">
					</ljshop-goods-card>
				</view>
			</view>
		</view>
<!-- 
		<button v-show="hasmore" class="u-reset-button refresh-btn u-m-y-20 u-flex u-col-center u-row-center"
			@tap.stop="loadMore">
			<text class="u-m-r-6 u-iconfont uicon-reload" style="font-size: 28rpx;color: #999"
				:class="{ 'refresh-active': isRefresh }"></text>
			{{ hasmore ? '收起' : '加载更多' }}
		</button> -->
		<view class="x-c" style="width: 100%;">
			<u-loadmore v-show="hasmore" height="80rpx" :status="loadStatus" icon-type="flower" color="#ccc" />
		</view>
	</view>
</template>

<script>
	/**
	 * 自定义之为你推荐
	 * @property {Object} detail - 推荐商品信息
	 */
	import shGoodsCard from './sh-goods-card.vue';
	export default {
		components: {
			shGoodsCard
		},
		data() {
			return {
				page: 0,
				total: 0, //总商品数
				perPage: 0, //单页商品数
				goodsList: [],
				isRefresh: true,

				// 瀑布流 350-330
				addTime: 0, //排序间隙时间
				leftHeight: 0,
				rightHeight: 0,
				leftList: [],
				rightList: [],
				rebate:40,
				hasmore:true,
				loadStatus: 'loadmore', //loadmore-加载前的状态，loading-加载中的状态，nomore-没有更多的状态
				

			};
		},

		props: {
			detail: {
				type: Object,
				default: () => {}
			}
		},
		created() {
			this.getGoodsList()
			
			// 触底监听
			uni.$on('uOnReachBottom', data => {
				if (this.hasmore) {
					this.page += 1;
					this.getGoodsList();
				}
			});
		},

		methods: {
			async splitData() {

				if (!this.tempList.length) return;
				let item = this.tempList[0];
				if (!item) return;
				// 分左右
				if (this.leftHeight < this.rightHeight) {
					this.leftHeight += 330;
					item.subsidy =((item.price-item.cost)*this.rebate/100).toFixed(2);
					this.leftList.push(item);
				} else if (this.leftHeight > this.rightHeight) {
					this.rightHeight += 330;
					item.subsidy =((item.price-item.cost)*this.rebate/100).toFixed(2);
					this.rightList.push(item);
				} else {
					this.leftHeight += 330;
					this.leftList.push(item);
				}

				// 移除临时列表的第一项，如果临时数组还有数据，继续循环
				this.tempList.splice(0, 1);
				if (this.tempList.length) {
					setTimeout(() => {
						this.splitData();
					}, this.addTime);
				}


			},
			clear() {
				this.leftList = [];
				this.rightList = [];
				this.leftHeight = 0;
				this.rightHeight = 0;

			},

			getGoodsList() {
				let that = this;
				that.$http('goods.hotlists', {
					page: that.page
				}).then(res => {
				 
					if (res) {
						if(res.product.length>0){
							
							if(that.page==0){
								 
								that.goodsList = res.product;
								that.tempList = res.product;
								that.rebate = res.rebate;
								that.goodsList.length && that.splitData();
							}
							else{
							 
								that.goodsList = res.product;
								that.tempList = res.product;
							    that.goodsList.concat(res.product)
								that.tempList.concat(res.product)
								that.goodsList.length && that.splitData();
							}
						}
						else{
							that.hasmore=false
						}
						
					}
				});
			},

			// 加载更多
			loadMore() {
				if (!this.isRefresh) {
					// 加载更多
					if (this.hasmore) {
						this.isRefresh = true;
						this.page += 1;
						this.getGoodsList();
					} else {
						// 重置为1页数据
						this.page = 0;
						this.lastPage = 0;
						this.goodsList = [];
						this.clear();
						this.getGoodsList();
					}
				}
			}
		}
	};
</script>

<style lang="scss">
	@mixin vue-flex($direction: row) {
		/* #ifndef APP-NVUE */
		display: flex;
		flex-direction: $direction;
		/* #endif */
	}

	.u-waterfall {
		@include vue-flex;
		flex-direction: row;
		align-items: flex-start;
	}

	.u-column {
		@include vue-flex;
		flex: 1;
		flex-direction: column;
		height: auto;
	}

	.u-image {
		width: 100%;
	}

	// 为你推荐
	.hot-goods {
		background: none;

		.refresh-btn {
			margin-left: 50%;
			transform: translateX(-50%);
			width: 156rpx;
			line-height: 50rpx;
			background: #ffffff;
			border-radius: 25rpx;
			font-size: 22rpx;
			font-weight: 500;
			color: #999999;
			white-space: nowrap;
		}

		.refresh-active {
			transform: rotate(360deg);
			transition: all linear 0.5s;
		}
	}
</style>
